<?php

class UserController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	
	public function actionRegister()
	{
		$model=new UserForm;

		// collect user input data
		if(isset($_POST['UserForm']))
		{
			$model->attributes=$_POST['UserForm'];
			$model->oldpass = $model->password;
			if( $model->register()){
				$this->redirect("login");
			}
		}else{
		// display the login form
		$this->render('register',array('model'=>$model));
		}
	}

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','create','profile','addFriend', 'updateProfile', 'home', 'updateProfile2'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->user->returnUrl);
	}
	
	public function actionProfile(){
		if(Yii::app()->user->isGuest){
			$this->redirect(Yii::app()->homeUrl);
		}
		
		$model=new Tbprofile;

		// collect user input data
		if(isset($_POST['Tbprofile']))
		{			
			//print_r($_POST['Tbprofile']);
			//die();
			$model->attributes=$_POST['Tbprofile'];
			$model->image=CUploadedFile::getInstance($model,'image');
			$error =$model->updateProfile();
			$this->redirect(Yii::app()->homeUrl);
	
		}else{
		$model = $model->getCurrentProfile(Yii::app()->user->getId()->Id);
		if(Yii::app()->user->getId()->Icon != null){
			$model->image = Tbprofile::model()->iconPath. Yii::app()->user->getId()->Icon;
		}
		$model->IdUser = Yii::app()->user->getId()->Id;
		$this->render('profile',array('model'=>$model));
		}
	}
	
	public function actionChangepass(){
	if(Yii::app()->user->isGuest){
			$this->redirect("user/login");
		}
		
		$model=new UserForm;

		// collect user input data
		if(isset($_POST['UserForm']))
		{
			$model->attributes=$_POST['UserForm'];
			$model->changepass();
			if($model->error == ""){
				$this->redirect("index.php?r=user/login");
			}else{
				$model->password = "";
				$this->render('changepass',array('model'=>$model));
			}
	
		}else{
		$model->password = "";
		$this->render('changepass',array('model'=>$model));
		}
	}
}